<!--
 * @Description: 商家远程项目二次审核列表
 * @Author: liyujie
 * @Date: 2021/09/08
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/09/08
-->
<template>
    <PageLayout title="平台二次审核">
        <!-- 筛选-->
        <template slot="filter">
            <div class="filter-item">
                <p class="filter-item__title">
                    任务标题/编号
                </p>
                <el-input
                    v-model="filterData.task_keyword"
                    placeholder="请选择任务标题/编号"
                    suffix-icon="el-icon-search"
                    clearable
                    size="medium"
                    style="width: 220px"
                    @keydown.enter.native="getTableData(1)"
                />
            </div>
            <div class="filter-item">
                <p class="filter-item__title">
                    公司名称/编号
                </p>
                <el-input
                    placeholder="请输入公司名称/编号"
                    v-model="filterData.company_keyword"
                    suffix-icon="el-icon-search"
                    clearable
                    size="medium"
                    style="width: 220px"
                    @keydown.enter.native="getTableData(1)"
                />
            </div>
            <div class="filter-item">
                <p class="filter-item__title">
                    用户信息
                </p>
                <el-input
                    placeholder="请输入用户姓名/编号"
                    v-model="filterData.student_keyword"
                    suffix-icon="el-icon-search"
                    clearable
                    size="medium"
                    style="width: 220px"
                    @keydown.enter.native="getTableData(1)"
                />
            </div>
            <div class="filter-item">
                <p class="filter-item__title">
                    任务类型
                </p>
                <el-select
                    v-model="filterData.task_category"
                    size="medium"
                    style="width: 152px"
                    @change="getTableData(1)"
                >
                    <el-option
                        v-for="item in typeList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </div>
            <div class="filter-item">
                <p class="filter-item__title">
                    处理状态
                </p>
                <el-select
                    v-model="filterData.status"
                    size="medium"
                    style="width: 152px"
                    @change="getTableData(1)"
                >
                    <el-option
                        v-for="item in verifyStatusList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </div>
            <div class="filter-item">
                <p class="filter-item__title">
                    处理人
                </p>
                <el-input
                    placeholder="请输入处理人信息"
                    v-model="filterData.admin_true_name"
                    suffix-icon="el-icon-search"
                    clearable
                    size="medium"
                    style="width: 220px"
                    @keydown.enter.native="getTableData(1)"
                />
            </div>
            <div class="filter-item">
                <p class="filter-item__title">
                    提交时间
                </p>
                <el-date-picker
                    v-model="dateTimeRange"
                    type="datetimerange"
                    range-separator="-"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    :default-time="['00:00:00', '23:59:59']"
                    size="medium"
                    style="width: 360px"
                    value-format="timestamp"
                    @change="getTableData(1)"
                />
            </div>
            <div class="filter-item">
                <el-button
                    type="primary"
                    size="small"
                    class="query-button"
                    @click="getTableData(1)"
                >
                    查询
                </el-button>
                <el-button
                    type="text"
                    @click="handleReset()"
                >
                    重置
                </el-button>
            </div>
        </template>
        <!--筛选-->
        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            ref="table"
            class="data-table"
            :cell-style="{height: '40px'}"
            v-loading="tableLoading"
        >
            <el-table-column
                label="序号"
                align="center"
                width="80"
                type="index"
                fixed="left"
            />
            <el-table-column
                prop="task_title"
                label="任务信息"
                width="220"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <div style="padding: 6px 0">
                        <div class="cell-title">{{ scope.row.task_title }}</div>
                        <div class="cell-tips">任务ID：{{ scope.row.task_no }}</div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                label="任务类型"
                align="center"
            >
                <template slot-scope="scope">
                    {{ scope.row.task_category | typeFilter }}
                </template>
            </el-table-column>
            <el-table-column
                prop="company_name"
                label="公司信息"
                width="150"
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="task_price"
                label="单任务酬劳"
                width="110"
                align="center"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    ￥{{ scope.row.task_price }}
                </template>
            </el-table-column>
            <el-table-column
                label="任务时间"
                align="left"
                width="200"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <template v-if="scope.row.apply_start_time !== -1">
                        <div>起始：{{ scope.row.apply_start_time * 1000 | dateFormatter('yyyy-MM-dd HH:mm:ss') }}</div>
                        <div>截止：{{ scope.row.apply_end_time * 1000 | dateFormatter('yyyy-MM-dd HH:mm:ss') }}</div>
                    </template>
                    <template v-else>
                        <div>起始：{{ scope.row.start_time * 1000 | dateFormatter('yyyy-MM-dd HH:mm:ss') }}</div>
                        <div>截止：{{ scope.row.end_time * 1000 | dateFormatter('yyyy-MM-dd HH:mm:ss') }}</div>
                    </template>
                </template>
            </el-table-column>
            <el-table-column
                label="用户信息"
                width="180"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <div style="padding: 6px 0">
                        <div class="cell-title">{{ scope.row.true_name }}</div>
                        <div class="cell-tips">手机号：{{ scope.row.phone }}</div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="school_name"
                label="学校名称"
                width="150"
                :formatter="emptyFormatter"
            />
            <el-table-column
                label="提交审核时间"
                align="left"
                width="160"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <div>{{ scope.row.create_time * 1000 | dateFormatter('yyyy-MM-dd HH:mm:ss') }}</div>
                </template>
            </el-table-column>
            <el-table-column
                label="处理状态"
                align="center"
            >
                <template slot-scope="scope">
                    <div class="status-wrapper">
                        <div
                            class="status-dot"
                            :class="['status-dot', scope.row.status === 1 ? 'success' : scope.row.status === 0 ? 'warning' : scope.row.status === -1 ? 'danger' : 'normal']"
                        />
                        <div>{{ scope.row.status | statusFilter }}</div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="verify_admin_true_name"
                label="处理人"
                :formatter="emptyFormatter"
            />
            <el-table-column
                label="操作"
                align="left"
                fixed="right"
                width="100"
            >
                <template slot-scope="scope">
                    <div class="option-btn-group">
                        <el-button
                            type="text"
                            size="medium"
                            @click="handleRoute(scope.row.task_category === 10 ? 'shopProjectSecondaryVerifyQuestionDetail' : 'shopProjectSecondaryVerifyDetail', {
                                task_no: scope.row.task_no,
                                apply_no: scope.row.apply_no
                            })"
                        >
                            {{ scope.row.status === 0 ? '审核' : '查看' }}
                        </el-button>
                        <!--<el-button
                            type="text"
                            size="medium"
                            :disabled="scope.row.verify !== 1"
                            @click="showVerifyDialog(2, scope.row)"
                        >
                            通过
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            :disabled="scope.row.verify !== 1"
                            @click="showVerifyDialog(-1, scope.row)"
                        >
                            不通过
                        </el-button>-->
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->
        <!-- /分页 -->
        <template slot="pagination">
            <div/>
            <el-pagination
                layout="total, prev, pager, next, jumper"
                background
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </template>
    </PageLayout>
</template>

<script>
import { back } from '@/mixin/back';
import {selectAgentLevelDialog} from "@/components/dialog/select-agent-level";

export default {
    name: 'SecondaryVerify',
    mixins: [back],
    components: {},
    data() {
        return {
            filterData: {
                task_keyword: '',
                company_keyword: '',
                student_keyword: '',
                admin_true_name: '',
                status: 99,
                task_category: 99,
                start_time: '',
                end_time: ''
            },
            tableData: [],
            tableLoading: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            dateTimeRange: null,
            verifyStatusList: [
                {label: '全部', value: 99},
                {label: '待审核', value: 0},
                {label: '已通过', value: 1},
                {label: '未通过', value: -1},
            ],
            typeList: [
                {label: '全部', value: 99},
                {label: '远程任务', value: 3},
                {label: '推广引流', value: 9},
                {label: '问卷调查', value: 10},
            ],
            authResult: {}
        };
    },
    computed: {},
    filters: {
        statusFilter(val) {
            switch (val) {
                case 0:
                    return '待审核';
                case 1:
                    return '已通过';
                case -1:
                    return '未通过';
                default:
                    return '--';
            }
        },
        typeFilter(val) {
            switch (val) {
                case 3:
                    return '远程任务';
                case 9:
                    return '推广引流';
                case 10:
                    return '问卷调查';
                default:
                    return '--';
            }
        },
    },
    watch: {},
    mounted() {
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '工作'}, {title: '商家远程任务'}, {title: '平台二次审核'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },

    methods: {

        /**
         * @description 获取列表数据
         * @param page
         */
        getTableData(page) {
            this.tableLoading = true;
            let params = { ...this.filterData };

            params.page_id = page || this.page.now_page;
            params.start_time = this.dateTimeRange ? this.dateTimeRange[0] / 1000 : '';
            params.end_time = this.dateTimeRange ? this.dateTimeRange[1] / 1000 : '';
            this.$post('/student/task_stuagent_for_shop%5Cget_receive_list', params, res => {
                if (res.code === 1) {
                    this.tableData = res.data.list;
                    this.page = res.data.page;
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
                this.tableLoading = false;
            });
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         */
        handleRoute(routeName, query) {
            this.$router.push({
                name: routeName,
                query
            });
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 重置筛选条件
         */
        handleReset() {
            this.filterData = {
                task_keyword: '',
                company_keyword: '',
                student_keyword: '',
                admin_true_name: '',
                status: 99,
                start_time: '',
                end_time: ''
            };
            this.dateTimeRange = null;
            this.getTableData(1);
        },

        /**
         * 显示审核操作弹窗
         * @param action -1拒绝 2 通过
         * @param row
         */
        async showVerifyDialog(action, row) {
            if(!this.authResult.auth_verify) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            // 通过
            if (action === 2) {
                this.$zdConfirm('是否确认通过该任务的审核？', '审核通过提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    let result = await selectAgentLevelDialog({
                        title: '设置用户领取门槛',
                        message: '领取用户群体',
                        hasCancelButton: false
                    });

                    if(typeof result === 'number') {
                        this.handleVerify(row.task_no, action, '', result);
                    }
                }).catch(() => {});
            } else {
                let reason = await this.$zdPrompt({
                    title: '任务不通过',
                    message: '不通过原因',
                    placeholder: '请输入不通过原因…'
                });

                if(reason) {
                    this.handleVerify(row.task_no, action, reason);
                }
            }
        },

        /**
         * 审核
         * @param task_no
         * @param verify
         * @param verify_fail_reason
         * @param submit_agent_level
         */
        handleVerify(task_no, verify, verify_fail_reason, submit_agent_level = '') {
            let params = {
                task_no,
                verify,
                verify_fail_reason,
                submit_agent_level
            };

            this.$post('/student/task_stuagent_for_shop%5Caudit', params, resp => {
                if (resp.code === 1) {
                    this.$notify({
                        title: '提示',
                        message: '操作成功',
                        duration: 2000,
                        type: 'success'
                    });
                    this.getTableData();
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },
    }
};
</script>
